﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FacebookWrapper.ObjectModel;
using System.Xml.Serialization;
using System.IO;
using System.Threading;
using FacebookWrapper;

namespace B12_Ex01_Guy_066473471_Erez_021616735.FacebookApplicationLogic
{
    class FacebookApplication
    {
        public User LoggedInUser { get; set; }
        public String AccessToken { get; set; }

        private Thread SaveToFileThread;
        
        /// <summary>
        /// The constructor set the user and keeps all 
        /// its friends to file in other Thread
        /// </summary>
        /// <param name="i_LoggedInUser"></param>
        public FacebookApplication(User i_LoggedInUser)
        {
            this.LoggedInUser = i_LoggedInUser;
            SaveToFileThread = new Thread(new ThreadStart(this.saveingFriendsInFile));
            SaveToFileThread.Start();
        }

       /// <summary>
       /// This function requests all friends from  API FACEBOOK 
       /// </summary>
       /// <returns>return list of friends from type User</returns>
        public List<User> getFriends()
        {
            return LoggedInUser.Friends.ToList();
        }

        /// <summary>
        /// This function requests all friends from  API FACEBOOK 
        /// </summary>
        /// <returns>return list of Male friends from type User</returns>
        public List<User> getMaleFriends()
        {
            List<User> maleFriends = new List<User>();

            foreach (User friend in getFriends())
            {
                if (friend.Gender == FacebookWrapper.ObjectModel.User.eGender.male)
                    maleFriends.Add(friend);
            } 
            
            return maleFriends;
        }

        /// <summary>
        /// This function requests all friends from  API FACEBOOK 
        /// </summary>
        /// <returns>return list of Female friends from type User</returns>
        public List<User> getFemaleFriends()
        {
            List<User> maleFriends = new List<User>();

            foreach (User friend in getFriends())
            {
                if (friend.Gender == FacebookWrapper.ObjectModel.User.eGender.female)
                    maleFriends.Add(friend);
            }

            return maleFriends;
        }

        /// <summary>
        /// This function requests all friends from  API FACEBOOK 
        /// //key = id,val = user name
        /// </summary>
        /// <returns>return Dictionary of friends from type User</returns>
        public Dictionary<String, String> getFriendsName()
        {     
            Dictionary<String, String> namesFrinds = new Dictionary<String, String>();
            
            foreach(User friend in LoggedInUser.Friends)
            {
                namesFrinds.Add(friend.Id, friend.Name);
            }

            return namesFrinds;
        }

        /// <summary>
        /// This function will requests all the friends of Facebook, 
        /// and compare with a list of friends that she kept on file from previous wheelbarrows
        /// </summary>
        /// <returns>return a list of all members of your ex friends</returns>
        public List<String> getExFriendsNames()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<String>));
            TextReader textReader;
            List<String> saveFrindes;
            List<String> ExFriends = null;

            try
            {
                textReader = new StreamReader(LoggedInUser.Id + ".xml");
                saveFrindes = (List<String>)serializer.Deserialize(textReader);
                textReader.Close();
            }
            catch (IOException ex)
            {
                return ExFriends;
            }

            ExFriends = new List<string>();
            var currFrindes = this.getFriendsName();

            foreach (String nameNreind in saveFrindes)
            {
                if (currFrindes.ContainsValue(nameNreind) == false)
                {
                    ExFriends.Add(nameNreind);
                }
            }

            return ExFriends;
        }
        

        /// <summary>
        /// This function will save all your friends on file in the file name will be your ID
        /// </summary>
        private void saveingFriendsInFile()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<String>));
            TextReader textReader;
            List<String> saveFrindes;

            try
            {
                textReader = new StreamReader(LoggedInUser.Id + ".xml");
                saveFrindes = (List<String>)serializer.Deserialize(textReader);
                textReader.Close();
            }
            catch (IOException ex)
            {
                saveFrindes = new List<String>();
            }

            var currFrindes = this.getFriendsName();

            foreach (KeyValuePair<string, string> freind in currFrindes)
            {
                if (saveFrindes.Contains(freind.Value) == false)
                {
                    saveFrindes.Add(freind.Value);
                }
            }

            TextWriter textWriter = new StreamWriter(LoggedInUser.Id + ".xml");
            serializer.Serialize(textWriter, saveFrindes);
            textWriter.Close();
        }
    }
}
